package org.haifi.game.service.impl;

import java.util.Set;

import javax.annotation.PostConstruct;

import org.haifi.game.service.WordFilterManager;
import org.haifi.game.util.XlsUtil;
import org.springframework.stereotype.Component;

/**
 * 过滤敏感词
 * 
 * @author joycube2
 * 
 */
@Component
public class WordFilterManagerImpl implements WordFilterManager {

	@Override
	public String filter(String text) {
		for (String word : dirtyWords) {
			if (text.contains(word)) {
				String replace = "";
				for (int m = 0; m < word.length(); m++) {
					replace += "*";
				}
				text = text.replaceAll(word, replace);
			}
		}
		return text;
	}

	@Override
	public boolean isNameDirty(String text) {
		return isNameDirty(dirtyNameWords, text)
				|| isNameDirty(dirtyWords, text);
	}

	private boolean isNameDirty(Set<String> words, String text) {
		for (String word : words) {
			if (text.contains(word)) {
				return true;
			}
		}
		return false;
	}

	@PostConstruct
	public void init() {
		dirtyWords = XlsUtil.parse("filter_word.xls", "all");
		dirtyNameWords = XlsUtil.parse("filter_word_name.xls", "all");
	}

	/**
	 * 过滤字
	 */
	private Set<String> dirtyWords;

	/**
	 * 角色名额外的过滤字
	 */
	private Set<String> dirtyNameWords;

}
